import paginationConf from './pagination';
import { getAlarmList, addNewAlarm, updateAlarm } from '@/services/alarmManage';
import _ from 'lodash';

import { message } from 'antd';

const AlarmModel = {
  namespace: 'alarmManage',
  state: {
    alarmData: [], //预警列表
    loading: false,
    visible: false, //页面是否打开
    optiontype: null, //打开model操作类型
    alarmDetail: {},
    effectPageInfo: {},
    drawerVisible: false,
    pagination: {
      ...paginationConf,
    },
  },
  effects: {
    *getData({ payload }, { call, put, select }) {
      const pagination = yield select((state) => state.monitorManage.pagination);
      const response = yield call(getAlarmList, { ...payload, pagination });
      const makerList = _.get(response, 'data.items', []);

      yield put({
        type: 'updateDataSet',
        payload: {
          makerList,
          pagination: {
            total: _.get(response, 'data.total', null),
            pageNum: _.get(response, 'data.page', 1),
          },
        },
      });
    },
    *submitMonitorInfo({ payload, callback }, { call, put }) {
      const { type, formData } = payload;
      // 判断是更新还是新增
      const callFn = type === 'add' ? addNewAlarm : updateAlarm;
      const ret = yield call(callFn, formData);
      callback(ret);
    },
    *deleteMonitorInfo({ payload, callback }, { put, call }) {
      const ret = yield call(deleteMonitor, payload);
      callback(ret);
    },
    *updateMonitorInfo({ payload, callback }, { put, call }) {
      const ret = yield call(updateMonitor, payload);
      callback(ret);
    },
    changePage({ payload }, { put }) {
      put({
        type: 'changePage',
        payload,
      });
    },
  },
  reducers: {
    //改变Table表加载状态
    changeLoadingStatus(state, { payload }) {
      return { ...state, loading: !state.loading };
    },
    updateDataSet(state, { payload }) {
      return {
        ...state,
        alarmData: payload.makerList,
        pagination: { ...state.pagination, ...payload.pagination },
        loading: false,
        effectPageInfo: { current: state.pagination.start },
      };
    },
    //打开Model时调用
    changeModalVisibleAndSetValue(state, { payload }) {
      if (payload.type === 'add') {
        return {
          ...state,
          visible: !state.visible,
          alarmDetail: {},
          optiontype: payload.type,
        };
      } else if (payload.type === 'update') {
        return {
          ...state,
          visible: !state.visible,
          optiontype: payload.type,
          alarmDetail:
            {
              ...payload.data,
            } || {},
        };
      }
      return { ...state, visible: !state.visible };
    },
    //关闭Model时调用
    changeModelVisibleAndCloseModel(state) {
      return { ...state, visible: !state.visible, optiontype: null, alarmDetail: {} };
    },

    changePage(state, { payload }) {
      return {
        ...state,
        pagination: { ...state.pagination, start: payload },
      };
    },
  },
};

export default AlarmModel;
